/**
 * 
 */
package com.couchcrowler.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.couchcrowler.server.dao.ProfileDao;
import com.couchcrowler.server.parser.ProfileParser;
import com.couchcrowler.shared.Profile;
import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * @author vienozin
 *
 */
@Singleton
public class ProfileParserServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2580529013202559427L;

	@Inject
	private ProfileParser parser;
	
	@Inject
	private ProfileDao profileDao;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String profileId = request.getParameter("id");
		if (profileId != null) {
			Profile profile = getParser().parseId(profileId);
			if (profile != null) {
				getProfileDao().save(profile);
			}
			
			response.setStatus(HttpServletResponse.SC_OK);
		} else {
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		}	
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	public ProfileParser getParser() {
		return parser;
	}

	public void setParser(ProfileParser parser) {
		this.parser = parser;
	}

	public ProfileDao getProfileDao() {
		return profileDao;
	}

	public void setProfileDao(ProfileDao profileDao) {
		this.profileDao = profileDao;
	}
	
	
}
